package mediadisplay.menusystem 
{
	import flash.display.GradientType;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	/**
	 * ...
	 * @author Anders Figenschow
	 */
	public class MenuField extends Sprite
	{
		private var _txt:String;
		private var _url:String;
		private var _menu:Menu;
		
		private var tf:TextField;
		private var textformat:TextFormat;
		
		private var holder:Sprite;
		private var _tuneID:int;
		
		public function MenuField(menu:Menu, tuneID:int, txt:String, url:String) 
		{
			_tuneID = tuneID;
			_menu = menu;
			_url = url;
			_txt = txt;
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			holder = new Sprite();
			addChild(holder);
			
			var fType:String = GradientType.LINEAR;
			var colors:Array = [ MenuSettings.MENU_FILL_COLOR, 0 ];
			var alphas:Array = [ 1, 1 ];
			var ratios:Array = [ 0, 255 ];
			var matr:Matrix = new Matrix();
			matr.createGradientBox( 40, 100, 0, 0, 0 );
			matr.rotate(Math.PI/2);
			var sprMethod:String = SpreadMethod.PAD;
			
			holder.graphics.lineStyle(1, MenuSettings.MENU_FRAME_COLOR);
			holder.graphics.beginGradientFill( fType, colors, alphas, ratios, matr, sprMethod );
			holder.graphics.drawRect(0, 0, MenuSettings.MENU_WIDTH, 20);
			holder.graphics.endFill();
			
			textformat = new TextFormat();
			textformat.size = 14;
			textformat.color = 0;
			
			tf = new TextField();
			holder.addChild(tf);
			tf.defaultTextFormat = textformat;
			tf.autoSize = TextFieldAutoSize.LEFT;
			tf.mouseEnabled = false;
			tf.selectable = false;
			
			tf.text = _txt;
			
			addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
			addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
			addEventListener(MouseEvent.CLICK, mouseClickHandler);
			holder.buttonMode = true;
		}
		
		private function mouseClickHandler(e:MouseEvent):void 
		{
			trace("clicked for URL: " + _url);
			_menu.main.changeTune(_tuneID, _txt, _url);
		}
		
		private function mouseOverHandler(e:MouseEvent):void 
		{
			textformat = new TextFormat();
			textformat.size = 14;
			textformat.color = 0xff0000;
			tf.setTextFormat(textformat);
		}
		
		private function mouseOutHandler(e:MouseEvent):void 
		{
			textformat = new TextFormat();
			textformat.size = 14;
			textformat.color = 0;			
			tf.setTextFormat(textformat);
		}
		
	}

}